import 'dart:async';
import '../../main.dart';
import '../api/api.dart';
import '../utils/log.dart';
import '../utils/secure_storage.dart';
import '../utils/x_cache.dart';
import 'event_entity.dart';

// 事件上报
class EventService {
  static EventService instance = EventService();

  final List<EventEntity> _eventList = [];
  final List<EventEntity> _pageEventList = [];

  static int openBatchId = DateTime.now().microsecondsSinceEpoch;
  static int openBatchIdPage = DateTime.now().microsecondsSinceEpoch;

  static String todayTimestamp = "";

  ///使用的天数
  static int installDay = 0;

  int userRegisterTime = 0;

  ///保存记录的时间的时间
  static int saveUserTimeTimeTime = 0;

  int _times = 0;

  EventService() {
    EventService.saveUserTimeTimeTime = 0;
    //getUserTodayUseTime();

    ///用户D时间
    //UserManager().retainDate = "D${EventService.installDay}";
   // getRetentionEvent();
    _startTask();
  }

  void sendEvent(String eventName) {
    sendEventEntity(
        EventEntity(eventName: eventName, openBatchId: openBatchId));
  }

  void sendEventEntity(EventEntity event) {
    _eventList.add(event);
  }

  ///实时上报
  void sendRealTimeEventEntity(String eventName, {int eventSource = 0}) {
    final event = EventEntity(
        eventName: eventName,
        openBatchId: openBatchId,
        eventSource: eventSource);
    final List<EventEntity> eventList = [event];
    List<EventEntity> list = List.from(eventList);

    Log.e("实时上报");
    // if (UserManager().isLogin()) {
    //   Api.batchEventReport(eventList: list);
    // }
  }

  ///实时上报page
  void _sendRealTimeEventPageEntity(
      String pageName, String eventName, int duration,
      {int eventSource = 0}) {
    final pageEvent = EventEntity(
        openBatchId: openBatchIdPage,
        pageName: pageName,
        eventName: eventName,
        duration: duration,
        eventSource: eventSource);
    final List<EventEntity> pageEventList = [pageEvent];
    List<EventEntity> list = List.from(pageEventList);

    // if (UserManager().isLogin()) {
    //   Api.batchEventPageLogAsync(eventList: list);
    // }
  }

  ///000000000000000上报页面统计时间000000000000000
  void sendPageEvent(String pageName, String eventName, int duration) {
    sendEventPageEntity(EventEntity(
        openBatchId: openBatchIdPage,
        pageName: pageName,
        eventName: eventName,
        duration: duration));
  }

  void sendEventPageEntity(EventEntity event) {
    _pageEventList.add(event);
  }

  void _startTask() {
  //  appStartTime = DateTime.now().millisecondsSinceEpoch;

    /// 启动的时候报一次
    // _sendRealTimeEventPageEntity("app_activate", "per_30s", DateTime.now().millisecondsSinceEpoch - appStartTime);
    // /// 5 秒
    // Timer.periodic(Duration(seconds: 50000), (timer) {
    //   _times += 5;
    //
    //   if (_times % buriedLogsTime == 0) {
    //     _sendRealTimeEventPageEntity("app_activate", "per_30s", DateTime.now().millisecondsSinceEpoch - appStartTime);
    //     if (!appIsBackground) {
    //       userTodayOnline += buriedLogsTime * 1000;
    //     } else {
    //       userTodayBackgroundOnline += buriedLogsTime * 1000;
    //       ///切到后台保存时间
    //       saveTodayTimestamp();
    //     }
    //     Log.e("用户在线时长前台======$userTodayOnline=========================");
    //     Log.e("用户在线时长后台======$userTodayBackgroundOnline===============");
    //     ///前台时间
    //     _sendRealTimeEventPageEntity("user_today_online", "user_today_online", userTodayOnline);
    //     ///后台上报的时间
    //     _sendRealTimeEventPageEntity("user_today_background", "user_today_background", userTodayBackgroundOnline);
    //   }
    // });
    //
    // ///用户安装时间
    // userRegisterTime =
    //     XCache.getInstance().get(AppConfig.USER_REGISTER_TIME) ?? 0;
  }

  void _doSendEvents() async {
    try {
      if (_eventList.isNotEmpty) {
        List<EventEntity> list = List.from(_eventList);
       // Api.batchEventPageLogAsync(eventList: list);
        _eventList.clear();
      }

      if (_pageEventList.isNotEmpty) {
        List<EventEntity> list = List.from(_pageEventList);
        //Api.batchEventPageLogAsync(eventList: list);
        _pageEventList.clear();
      }
    } catch (e) {
      Log.d(e.toString());
    }
  }
}

